package flex.cc.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import flex.cc.PublicUtil;
import flex.cc.drive.dao.OrderDriverDao;
import flex.cc.store.dao.BookingInfoDao;
import flex.cc.store.dao.StoreActivityConfigDao;
import flex.cc.store.dao.StoreCouponDao;
import flex.cc.store.dao.StoreOrderDao;
import flex.cc.store.dao.StorePackageDao;
import flex.cc.store.dto.entity.StoreActivityConfigEntity;
import flex.cc.store.dto.entity.StoreCouponEntity;
import flex.cc.store.dto.entity.StoreOrderEntity;
import flex.cc.store.dto.entity.StorePackageEntity;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;

/**
 * 服务包中卡券使用  特殊规则判断工具类
 * @author R
 */
@Component
public class SpecialRuleUtil {
	@Autowired
	private StorePackageDao storePackageDao;
    @Autowired
    private StoreCouponDao storeCouponDao;
    @Autowired
    private StoreOrderDao storeOrderDao;
    @Autowired
    private StoreActivityConfigDao activityConfigDao;
    @Autowired
    private BookingInfoDao bookingInfoDao;
    @Autowired
    private OrderDriverDao orderDriverDao;
    @Autowired
	private DictCodeService dictCodeService;
	/**
	 * 特殊规则判断
	 * @param couponNum  卡券编码  
	 * @param carNo		   车牌号  
	 * @return
	 */
	public boolean judgeSpecialRule(String couponNum,String carNo){
		//查flex_dict_code表中预约开关配置
		System.out.println("-----查询flex_dict_code----");
		Map<String, Object> codeParams = new HashMap<String, Object>();
		codeParams.put("code", "bookingSwitch");
		codeParams.put("typeCode", "storeSwitch");
		DictCodeEntity codeVo = dictCodeService.getOneByParams(codeParams);
		if(codeVo!=null && codeVo.getValue().equals("1")){
			//根据卡券编码获取卡券信息
			StoreCouponEntity storeCouponEntity = storeCouponDao.queryByCouponNum(couponNum);
			//服务包订单号
			String orderId = storeCouponEntity.getOrderId();
			//服务项目
			String activityId = storeCouponEntity.getActivityId();
			//服务商
			String provider = storeCouponEntity.getServer();
			//服务包编码
			String packageCodeCF = storeCouponEntity.getPackageCodeCF();

			//根据orderId获取服务包订单
			StoreOrderEntity storeOrderEntity = storeOrderDao.queryByOrderId(orderId);
			//服务包配置编码
			String configCode = storeOrderEntity.getServiceItemCode();
			
			Map<String, Object> reqVo = new HashMap<String, Object>();
			reqVo.put("configCode", configCode);
			reqVo.put("activityCode", activityId);
			reqVo.put("provider", provider);
			List<StoreActivityConfigEntity> activityConfigEntityList = activityConfigDao.queryActivityConfig(reqVo);
			if(activityConfigEntityList.size()>0){
				StoreActivityConfigEntity activityConfigEntity = activityConfigEntityList.get(0);
				//特殊规则
				String specialRules = activityConfigEntity.getSpecialRule();
				//服务类型  1:预约类  2:咨询类 3:医护类  4:代驾类
				String activityType = activityConfigEntity.getActivityType();
				
				if(activityType.equals("2")||activityType.equals("3")){
					return true;
				}
				
				String[] specialRuleArr = specialRules.split(",");
				for (int i = 0; i < specialRuleArr.length; i++) {
					String specialRule = specialRuleArr[i];
					if(specialRule.equals("1")){
						/**
						 * 核销校验车牌
						 * 卡券使用无需进行判断  代驾类预约和核销是需进行判断
						 */
						continue;
					}else if(specialRule.equals("2")){
						/**
						 * 每月限制一张
						 * 根据卡券的服务项目类型和归属服务包编码查询服务包下所有符合的卡券信息	
						 * 	如果其中卡券存在当前月使用的情况，提示客户“本月使用次数已达到上限，详情请查看\"使用规则\"说明”
						 */
						List<StorePackageEntity>  storePackageEntityList = storePackageDao.queryByPackageCodeCF(packageCodeCF);	
						String packageCodes = "";
						for (int j = 0; j < storePackageEntityList.size(); j++) {
							StorePackageEntity storePackageEntity = storePackageEntityList.get(j);
							String packageCode = storePackageEntity.getPackageCode();
							packageCodes += packageCode+",";
						}
						String[] packageCodeArr = packageCodes.split(",");
						
						if(PublicUtil.isEmpty(carNo)){
							carNo = storeCouponEntity.getCarNo();
						}
						
						Map<String, Object> checkParams = new HashMap<>();
	    		        checkParams.put("carNo", carNo);
	    		        checkParams.put("activityId", activityId);
	    		        checkParams.put("packageCodeArr", packageCodeArr);
	    		        checkParams.put("provider", provider);
		    		    
		    		    if(activityType.equals("1")){//预约类
		    		    	int checkBooking = bookingInfoDao.checkCouponRule(checkParams);
		    		        // TODO 使用规则
		    		        if (checkBooking >= 1) {
		    		            return false;
		    		        }
						}else if(activityType.equals("4")){//代驾类
							 int checkDriver = orderDriverDao.checkCouponRule(checkParams);
			    		     // TODO 使用规则
			    		     if (checkDriver >= 1) {
			    		    	 return false;
			    		     }
						}
					}else{
						/**
						 * 组合每月限制一张
						 * 根据卡券的归属服务包编码查询服务包下所有勾选了“组合每月限制一张”的服务项目  组合查询所有符合的卡券信息	
						 * 	如果其中卡券存在当前月使用的情况，提示客户“本月使用次数已达到上限，详情请查看\"使用规则\"说明”
						 */
						//获取服务包配置下所有的勾选“组合每月限制一张”的服务项目
						Map<String, Object> reqMap = new HashMap<String, Object>();
						reqMap.put("configCode", configCode);
						List<StoreActivityConfigEntity> storeActivityConfigEntityList = activityConfigDao.queryActivityConfig(reqMap);
						String activityIds = "";
						for (int j = 0; j < storeActivityConfigEntityList.size(); j++) {
							StoreActivityConfigEntity storeActivityConfigEntity = storeActivityConfigEntityList.get(j);
							String rule = storeActivityConfigEntity.getSpecialRule();
							String activityCode = storeActivityConfigEntity.getActivityCode();
							if(rule.contains("3")){
								activityIds += activityCode+",";
							}
						}
						String[] activityIdArr = activityIds.split(",");
						
						
						List<StorePackageEntity>  storePackageEntityList = storePackageDao.queryByPackageCodeCF(packageCodeCF);	
						String packageCodes = "";
						for (int j = 0; j < storePackageEntityList.size(); j++) {
							StorePackageEntity storePackageEntity = storePackageEntityList.get(j);
							String packageCode = storePackageEntity.getPackageCode();
							packageCodes += packageCode+",";
						}
						String[] packageCodeArr = packageCodes.split(",");
						
						if(PublicUtil.isEmpty(carNo)){
							carNo = storeCouponEntity.getCarNo();
						}
						
						Map<String, Object> checkParams = new HashMap<String, Object>();
	    		        checkParams.put("carNo", carNo);
	    		        checkParams.put("activityIdArr", activityIdArr);
	    		        checkParams.put("packageCodeArr", packageCodeArr);
	    		        checkParams.put("provider", provider);
	    		        int checkBooking = bookingInfoDao.checkCouponRule(checkParams);
	    		        int checkDriver = orderDriverDao.checkCouponRule(checkParams);
	    		        
	    		        // TODO 使用规则
		    		    if ((checkBooking+checkDriver) >= 1) {
		    		    	return false;
		    		    }
					}
				}
			}
		}
		
		
		return true;
	}
}
